package com.shijun.im.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.sdk.android.media.upload.UploadListener;
import com.alibaba.sdk.android.media.upload.UploadTask;
import com.alibaba.sdk.android.media.utils.FailReason;
import com.shijun.im.activity.activity.MainView;
import com.shijun.im.application.MyAppLication;
import com.shijun.im.bean.RequestTokenBean;
import com.shijun.im.R;
import com.shijun.im.http.APIInterface;
import com.shijun.im.http.CustomConverterFactory;
import com.shijun.im.http.RetrofitService;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;

public class MainActivity extends AppCompatActivity implements MainView{
    private APIInterface service;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    private void request() {
        service = new RetrofitService<RequestTokenBean>(RequestTokenBean.class).getRectService();
        Call<RequestTokenBean> model = service.repo("shijun", "123456");

        model.enqueue(new Callback<RequestTokenBean>() {
            @Override
            public void onResponse(Call<RequestTokenBean> call, Response<RequestTokenBean> response) {
                Log.i("TAG", "Token:" + response.toString());
                Log.i("TAG", "Token:" + response.body().toString());

                String token = response.body().getToken(); //从开发者服务端请求获取的凭证token
                doUpLoadImage(null, token);
            }

            @Override
            public void onFailure(Call<RequestTokenBean> call, Throwable t) {
                System.out.print(t.getMessage());
            }
        });
    }

    private void doUpLoadImage(File file, String token) {
        ((MyAppLication) getApplication()).getWantuService().upload(file, new UploadListener() {
            @Override
            public void onUploading(UploadTask uploadTask) {
                Log.i("TAG", "getCurrent:" + uploadTask.getCurrent());
            }

            @Override
            public void onUploadFailed(UploadTask uploadTask, FailReason failReason) {

            }

            @Override
            public void onUploadComplete(UploadTask uploadTask) {
                Log.i("TAG", "getCurrent:" + uploadTask.getResult().url);
            }

            @Override
            public void onUploadCancelled(UploadTask uploadTask) {

            }
        }, token);
    }

    private void upload(File file) {
        // 创建 RequestBody，用于封装构建RequestBody
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

        // MultipartBody.Part  和后端约定好Key，这里的partName是用image
        MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);

        // 添加描述
        String descriptionString = "hello, 这是文件描述";
        RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), descriptionString);

        // 执行请求 放MultipartBody.Part参数的
        Call<ResponseBody> call = service.upload(description, body);

//        放RequestBody参数的
//        Call<ResponseBody> call = service.uploadFile(requestFile);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call,
                                   Response<ResponseBody> response) {
                Log.v("Upload", "success");
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.e("Upload error:", t.getMessage());
            }
        });
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void showToast(String content) {

    }

    @Override
    public void showMenu() {

    }

    @Override
    public void turnToPersonalCenter() {

    }

    @Override
    public void cutFramentModule() {

    }

/*
*
MediaType.parse主要有一下几种（不完全）

application/json：JSON格式的数据，在RFC 4627中定义
application/javascript：JavaScript，在RFC 4329中定义但是不被IE8以及之前的版本支持

audio/mp4：MP4音频
audio/mpeg：MP3 或者MPEG音频，在RFC 3003中定义

image/jpeg：JPEG 和JFIF格式，在RFC 2045 和 RFC 2046中定义
image/png：png格式，在 RFC 2083中定义

text/html：HTML格式，在RFC 2854中定义
text/javascript ：JavaScript在已经废弃的RFC 4329中定义，现在推荐使用“application/javascript”。然而“text/javascript”允许在HTML 4 和5 中使用。并且与“application/javascript”不同，它是可以跨浏览器支持的。



* */


}
